package com.jeff.learn.slidewindow;

/**
 * {@link LongestSubStringWithoutRepeatingCharacters} 给定一个字符串，请找出其中不含有重复字符的最长子串的长度。
 * {@Description} 说明
 * {@Author} <b style="color:yellow">yaojianfeng</b> 2024/5/28 上午8:53
 */
public class LongestSubStringWithoutRepeatingCharacters {
    public static void main(String[] args) {
        String s = "abcabcbbeghtxopkkklmnopqrstuvwxyz";
        int length = lengthOfLongestSubstring(s);
        System.out.println(length);
    }

    public static int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int[] index = new int[128];
        int start = 0, end = 0, maxLen = 0;
        while (end < n) {
            char c = s.charAt(end);
            end++;
            while (index[c] > start) {
                index[s.charAt(start)] = start + 1;
                start++;
            }
            index[c] = end;
            maxLen = Math.max(maxLen, end - start);
        }
        return maxLen;
    }

}
